package com.oceansoft.core.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 时间处理类
 * 
 * @author Administrator
 * 
 */
public final class DatetimeUtil {
	public static String getCurrentTimeAsString() {
		return getTimeAsString(System.currentTimeMillis());
	}

	public static String getCurrentDateAsString() {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		dateFormat.applyPattern("yyyyMMdd");
		return dateFormat.format(new Date(date.getTime()));
	}

	public static String getCurrentDateAsString(String pattern) {
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		dateFormat.applyPattern(pattern);
		return dateFormat.format(new Date(date.getTime()));
	}

	public static Date getSysDateAll() {
		return new Date();
	}

	public static Date getSysDateOnly() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String time = df.format(new Date());
		Date date = null;
		try {
			date = df.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date getSysDate() {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = df.format(new Date());
		Date date = null;
		try {
			date = df.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String getTimeAsString(long timeMillis, String newFormat) {
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		dateFormat.applyPattern(newFormat);
		return dateFormat.format(new Date(timeMillis));
	}

	public static String getTimeAsString(long timeMillis) {
		return getTimeAsString(timeMillis, "yyyyMMddHHmmss");
	}

	public static long getTimeMillis(String timeString) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat();
		dateFormat.applyPattern("yyyyMMddHHmmss");
		Date dt = dateFormat.parse(timeString);
		return dt.getTime();
	}

	public static String format(long timeMillis) {
		DateFormat dateFormat = DateFormat.getDateTimeInstance(1, 2);
		return dateFormat.format(new Date(timeMillis));
	}

	public static long getTimeMillis(int days, int hour, int minutes) {
		return (days * 24 * 60 * 60 * 1000L + hour * 60 * 60 * 1000L + minutes * 60 * 1000L);
	}

	public static long getDays(long timeMillis) {
		return (timeMillis / 86400000L);
	}

	public static long getHours(long timeMillis) {
		return (timeMillis / 3600000L);
	}

	public static long getMinutes(long timeMillis) {
		return (timeMillis / 60000L);
	}

	public static String getDHM(long timeMillis) {
		long hours = timeMillis % 86400000L;
		long minutes = hours % 3600000L;

		return String.valueOf(getDays(timeMillis)) + "天 " + String.valueOf(getHours(hours)) + "小时: "
				+ String.valueOf(getMinutes(minutes)) + "分钟";
	}

	public static int getHourOfDay(long timeMillis) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(timeMillis));
		return calendar.get(11);
	}

	public static int getMinite(long timeMillis) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(timeMillis));
		return calendar.get(12);
	}

	public static int getSecond(long timeMillis) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(timeMillis));
		return calendar.get(13);
	}

	public static String formatDate(Date date, String newFormat) {
		if ((date == null) || (newFormat == null)) {
			return null;
		}

		SimpleDateFormat formatter = new SimpleDateFormat(newFormat);
		return formatter.format(date);
	}

	public static Date formatString(String stringDate, String formatString) {
		if (stringDate == null || "".equals(stringDate)) {
			return null;
		}
		if (formatString == null || "".equals(formatString)) {
			formatString = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat formatter = new SimpleDateFormat(formatString);

		try {
			return formatter.parse(stringDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取一个月的最后一天
	 * 
	 * @param dat
	 * @return
	 */
	public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
		String str = dat.substring(0, 8);
		String month = dat.substring(5, 7);
		int mon = Integer.parseInt(month);
		if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
			str += "31";
		} else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
			str += "30";
		} else {
			if (isLeapYear(dat)) {
				str += "29";
			} else {
				str += "28";
			}
		}
		return str;
	}

	/**
	 * 判断是否润年
	 * 
	 * @param ddate
	 * @return
	 */
	public static boolean isLeapYear(String ddate) {

		/**
		 * 详细设计： 1.被400整除是闰年，否则： 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
		 * 3.能被4整除同时能被100整除则不是闰年
		 */
		Date d = strToDate(ddate);
		GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
		gc.setTime(d);
		int year = gc.get(Calendar.YEAR);
		if ((year % 400) == 0)
			return true;
		else if ((year % 4) == 0) {
			if ((year % 100) == 0)
				return false;
			else
				return true;
		} else
			return false;
	}

	/**
	 * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDateLong(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	/**
	 * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateToStrLong(java.util.Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	/**
	 * 将短时间格式时间转换为字符串 yyyy-MM-dd
	 * 
	 * @param dateDate
	 * @return
	 */
	public static String dateToStr(java.util.Date dateDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = formatter.format(dateDate);
		return dateString;
	}

	/**
	 * 将短时间格式字符串转换为时间 yyyy-MM-dd
	 * 
	 * @param strDate
	 * @return
	 */
	public static Date strToDate(String strDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		ParsePosition pos = new ParsePosition(0);
		Date strtodate = formatter.parse(strDate, pos);
		return strtodate;
	}

	public static final String getDate(String pattern) {
		Date date = new Date();
		return getDate(date, pattern);
	}

	public static final String getDate(Date date, String pattern) {
		SimpleDateFormat df = null;
		String returnValue = "";
		if (date != null) {
			df = new SimpleDateFormat(pattern);
			returnValue = df.format(date);
		}
		return (returnValue);
	}

	public static Map<String, String> getLastWeek() {
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, String> map = new HashMap<String, String>();
		Calendar cal = Calendar.getInstance();
		int n = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (n == 0) {
			n = 7;
		}
		cal.add(Calendar.DATE, -(7 + (n - 1)));// 上周一的日期
		Date monday = cal.getTime();
		map.put("firstDay", sdf.format(monday).substring(0,10));

		cal.add(Calendar.DATE, 1);
		Date tuesday = cal.getTime();
		map.put("tuesday", sdf.format(tuesday));

		cal.add(Calendar.DATE, 1);
		Date wednesday = cal.getTime();
		map.put("wednesday", sdf.format(wednesday));

		cal.add(Calendar.DATE, 1);
		Date thursday = cal.getTime();
		map.put("thursday", sdf.format(thursday));

		cal.add(Calendar.DATE, 1);
		Date friday = cal.getTime();
		map.put("friday", sdf.format(friday));

		cal.add(Calendar.DATE, 1);
		Date saturday = cal.getTime();
		map.put("saturday", sdf.format(saturday).substring(0,10));

		cal.add(Calendar.DATE, 1);
		Date sunday = cal.getTime();
		map.put("lastDay", sdf.format(sunday).substring(0,10));
		System.out.println("星期一:"+map.get("firstDay"));
		System.out.println("星期日:"+map.get("lastDay"));
		return map;
	}
	public static Map<String, String> getLastMonth() {
		Map<String, String> map = new HashMap<String, String>();
		//获取前一个月第一天
		Calendar calendar1 = Calendar.getInstance();
		calendar1.add(Calendar.MONTH, -1);
		calendar1.set(Calendar.DAY_OF_MONTH,1);
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String firstDay = sdf.format(calendar1.getTime());
		System.out.println(firstDay);
		//获取前一个月最后一天
		Calendar calendar2 = Calendar.getInstance();
		calendar2.set(Calendar.DAY_OF_MONTH, 0);
		String lastDay = sdf.format(calendar2.getTime());
		map.put("firstDay",firstDay.substring(0,10));
		map.put("lastDay",lastDay.substring(0,10));
		System.out.println("月一:"+map.get("firstDay"));
		System.out.println("月日:"+map.get("lastDay"));
		return map;
	}

	public static Map<String, String> getLastQuarter(){
		Date date = new Date();
		Map<String, String> map = new HashMap<String, String>();
		//取得日历
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);

		// 日历减6个月,即上上季度
		calendar.add(Calendar.MONTH,-3);

		int year = calendar.get(Calendar.YEAR);// 上季度的年份
		int mouth = calendar.get(Calendar.MONTH)+ 1;// 上季度的月份
		System.out.println(mouth);
		String mmdd = "";// 月/日
		// 根据月份,判断是哪个季度
		switch( mouth){
			case 1:
			case 2:
			case 3:
				mmdd = "03-31"; // 第一季度最后一天是3/31
				break;
			case 4:
			case 5:
			case 6:
				mmdd = "06-30"; // 第二季度最后一天是6/30
				break;
			case 7:
			case 8:
			case 9:
				mmdd = "09-30";// 第三季度最后一天是9/30
				break;
			case 10:
			case 11:
			case 12:
				mmdd = "12-31";// 第四季度最后一天是12/31
				break;
		}
		calendar.add(Calendar.MONTH,-2);
		int firstmouth = calendar.get(Calendar.MONTH)+ 2;// 上季度的月份
		String firstmmd = "";// 月/日
		// 根据月份,判断是哪个季度
		switch( firstmouth){
			case 1:
			case 2:
			case 3:
				firstmmd = "01-01"; // 第一季度最后一天是3/31
				break;
			case 4:
			case 5:
			case 6:
				firstmmd = "04-01"; // 第二季度最后一天是6/30
				break;
			case 7:
			case 8:
			case 9:
				firstmmd = "07-01";// 第三季度最后一天是9/30
				break;
			case 10:
			case 11:
			case 12:
				firstmmd = "10-01";// 第四季度最后一天是12/31
				break;
		}
		String lastDate = String.valueOf(year) + "-" + mmdd;
		String firstDay = String.valueOf(year) + "-" + firstmmd;
		System.out.println(lastDate);
		System.out.println(firstDay);
		map.put("firstDay",firstDay);
		map.put("lastDay",lastDate);
		return map;

	}

	public static Date addDay(Date date,int days){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_YEAR, days);
		return cal.getTime();
	}

	public static Date addMonth(Date date,int months){
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, months);
		return cal.getTime();
	}

	public final static void main(String[] args) {
		getLastWeek();
		getLastMonth();
		getLastQuarter();
	}
}
